$(function () {

    //begin列表信息
    $("#examination_datagrid").datagrid({
        url: "/examination/list",
        columns: [[
            {field: "xxx", checkbox: 'true'},
            {field: "id", title: "编号", width: 100,},
            {field: "name", title: "姓名", width: 100},
            {field: "qq", title: "QQ", width: 100},
            {field: "tell", title: "电话", width: 100},
            {
                field: "interestClass", title: "意向班级", width: 100, formatter: function (value, row, index) {
                if (value) {
                    return value.name;
                } else {
                    return null;
                }
            }
            },
            {
                field: "testType", title: "考试类型", width: 100, formatter: function (value, row, index) {
                if (value) {
                    return value.name;
                } else {
                    return null;
                }
            }
            },
            {field: "testDate", title: "考试时间", width: 100},
            {
                field: "marketer", title: "营销人员", width: 100, formatter: function (value, row, index) {
                if (value) {
                    return value.username;
                } else {
                    return null;
                }
            }
            },
            {
                field: "inputUser", title: "录入人", width: 100, formatter: function (value, row, index) {
                if (value) {
                    return value.username;
                } else {
                    return null;
                }
            }
            },
            {
                field: "testResult", title: "考试结果", width: 100,
                formatter: function (value, row, index) {
                    if (row.testResult == 0) {
                        return '<font color="red">未通过</font>';
                    } else {
                        return '<font color="green">已通过</font>';
                    }
                }
            },
            {
                field: "testHandler", title: "处理人", width: 100,
                formatter: function (value, row, index) {
                    if (value) {
                        return value.username;
                    } else {
                        return "未处理";
                    }
                }
            },
            {field: "testNode", title: "备注", width: 100},
        ]],
        fit: true,
        fitColumns: true,
        pagination: true,
        rownumbers: true,
        singleSelect: true,
        striped: true,
        pageNumber: 1,
        pageSize: 3,
        pageList: [3, 5, 10, 20],
        toolbar: "#examination_tb"
    });
    //end列表信息

    //begin添加/编辑对话框
    $("#examination_dialog").dialog({
        width: 400,
        height: 300,
        buttons: "#examination_bb",
        closed: true
    });
    //end添加/编辑对话框

    //begin处理功能方法的对象
    var obj = {
        /* //begin添加按钮
         add: function () {
         $("#examination_form").form("clear");
         $("#examination_dialog").dialog("setTitle", "添加考试管理信息");
         $("#examination_dialog").dialog("open");
         },
         //end添加按钮*/

        /*   //begin编辑按钮
         edit: function () {
         $("#examination_form").form("clear");
         $("#examination_dialog").dialog("setTitle", "编辑考试管理信息");
         var row = $("#examination_datagrid").datagrid("getSelected");
         if (!row) {
         $.messager.alert('温馨提示', '请选择要操作的行!');
         return;
         }
         $("#examination_form").form("load", row);
         $("#examination_dialog").dialog("open");
         },
         //end编辑按钮*/

        //begin取消按钮
        cancel: function () {
            $("#examination_dialog").dialog("close");
        },
        //end取消按钮

        //begin保存按钮
        save: function () {
            var examinationId = $("#examinationId").val();
            var url;
            if (examinationId) {
                url = "/examination/update"
            }
            $("#examination_form").form("submit", {
                url: url,
                success: function (data) {
                    var data = $.parseJSON(data);
                    if (!data.success) {
                        $.messager.alert('温馨提示', data.msg);
                    } else {
                        $.messager.alert('温馨提示', "操作成功!");
                        $("#examination_dialog").dialog("close");
                        $("#examination_datagrid").datagrid("reload");
                    }
                }
            });
        },
        //end保存按钮

        //begin删除按钮
        remove: function () {
            var row = $("#examination_datagrid").datagrid("getSelected");
            if (!row) {
                $.messager.alert('温馨提示', '请选择要操作的行!');
                return;
            }
            $.messager.confirm('确认', '您确认要操作此行吗？', function (r) {
                if (r) {
                    //注意这里的id要从当前选中的行中获取
                    var examinationId = row.id;
                    $.get("/examination/delete", {examinationId: examinationId}, function (data) {
                        if (!data.success) {
                            $.messager.alert('温馨提示', data.msg);
                        } else {
                            $.messager.alert('温馨提示', '操作成功!');
                            $("#examination_datagrid").datagrid("reload");
                        }
                    });
                }
            });

        },
        //end删除按钮

        //begin刷新按钮
        reload: function () {
            $("#examination_datagrid").datagrid("reload")
        },
        //end刷新按钮

        //begin审核通过按钮
        change: function () {
            var row = $("#examination_datagrid").datagrid("getSelected");
            if (!row) {
                $.messager.alert('温馨提示', '请选择要操作的行!');
                return;
            }
            if (row.state != 1) {
                $.get("/examination/changeStateById", {id: row.id}, function (data) {
                    if (!data.success) {
                        $.messager.alert('温馨提示', data.msg)
                    } else {
                        $.messager.alert('温馨提示', '审核成功')
                        $("#examination_datagrid").datagrid("reload");
                    }
                })
            } else {
                $.messager.alert('温馨提示', '该同学已经通过考试了!')
            }
        },
        //end审核通过按钮

        //begin 高级查询按钮
        query: function () {
            var keywords = $("#keywords").textbox("getValue");
            var interestClassId = $("#interestClassId").combobox("getValue");
            var beginDate = $("#beginDate").datebox("getValue");
            var endDate = $("#endDate").datebox("getValue");
            $("#examination_datagrid").datagrid("load", {
                keywords: keywords,
                interestClassId: interestClassId,
                beginDate: beginDate,
                endDate: endDate
            })
        }

        //end 高级查询按钮
    };
    //end处理功能方法的对象

    //begin处理功能方法对象,实际调用方法
    $("a[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");
        obj[cmd]();
    });
    //end处理功能方法对象,实际调用方法

});